<?php
class LeaveAction extends Action{

	private $leave_state=array(
		0=>"审批中",1=>"已审批",2=>"已驳回",3=>"申请销假",4=>"同意销假",5=>"驳回销假"
	);
	
/**
  *查询请假
  *update author: zhanghao
  *update time  : 2014-11-13 12:08
  ********************************************/
	public function get_leave_list(){
		//init
		if(!Database($_REQUEST["cid"]))exit;
		$_REQUEST['count']=check_null(10,false,$_REQUEST['count']);
		$_REQUEST['page'] =check_null(1 ,false,$_REQUEST['page']);
		$_REQUEST['uid'] =check_null(40002,true,$_REQUEST['uid']);
		
		$rs=D("Leave")->get_leave_list($_REQUEST);
		if($rs){
			return_json('0',$rs);
		}else{
			return_json('40001');
		}
	}	
	
	//获取请假类型列表
	public function get_type_list(){
		if(!Database($_REQUEST['cid']))exit;
		$rs = D('Leave')->get_type_list();
		if($rs){
			return_json('0',$rs);
		}else{
			return_json('40001');
		}
	}
	
	//创建请假
	public function create_leave(){
		switch(''){
			case $_POST["leave_startTime"]:return_json(40051);return;
			case $_POST["leave_endTime"]:return_json(40052);return;
			case $_POST["leave_type"]:return_json(40053);return;
			case $_POST["leave_uid"]:return_json(40054);return;
		}
		if(!Database($_REQUEST["cid"]))exit;
		if(count($_FILES)>0){
			$arr=$this->image_Upload();
		}
		$rs=D("Leave")->create_leave($_REQUEST);
		if($rs){
			if($_POST['prove_image_path']!=''){
				$data['leave_id']=$rs;
				$data['prove_image_path']=$_POST['prove_image_path'];
				$re=M('leave_prove','','DB_MEETING')->add($data);
			}
			return_json(0);
		}else{
			return_json(-1);
		}
	}
	

	
	//获取任务详情
	public function get_leave_details(){
		switch(''){
			case $_REQUEST["leave_id"]:return_json(40055);return;
		}
		if(!Database($_REQUEST["cid"]))exit;
		$rs    = D('Leave')->get_leave_by_id($_REQUEST["leave_id"]);
		$log   = D('Leave')->get_logs($_REQUEST["leave_id"]);
		$prove = D('Leave')->get_prove($_REQUEST['leave_id']);
		if($rs){
			$rs['log']=$log==array()?null:$log;
			$rs['prove']=$prove==array()?null:C('LEAVE_PROVE_PATH').$_REQUEST['cid'].'/'.$prove;
			return_json('0',$rs);
		}else{
			return_json('40001');
		}
	}
	
	//修改当前请假条状态
	public function update_state(){
		switch(''){
			case $_REQUEST['uid']:return_json(40002);return;
			case $_REQUEST['leave_state']:return_json(40056);return;
			case $_REQUEST["leave_id"]:return_json(40055);return;
		}
		if($_REQUEST['leave_state']==3){
			if($_REQUEST['back_time']==0){
				return_json(40063);
				return;
			}
		}
		if(!Database($_REQUEST["cid"]))exit;
		$_REQUEST['time_update']=time();
		$rs=D('Leave')->update_state($_REQUEST);
		if($rs){
			return_json(0);
		}else{
			return_json(-1);
		}
	}
	
	//文件上传
	public function image_Upload(){
		$dataDir=C('LEAVE_PROVE_PATH');
		if(!is_dir($dataDir)){mkdir($dataDir,0777,true);}
		$dataDir.=$_REQUEST['cid'].'/';
		if(!is_dir($dataDir)){mkdir($dataDir,0777,true);}
		import("ORG.Net.UploadFile");
		$config['savePath'] = $dataDir;
		$upload = new UploadFile($config);
		if(!$upload->upload()){
			echo json_encode($upload->getErrorMsg());
		}else{
			$uploadList = $upload->getUploadFileInfo();
			$_POST['prove_image_path'] = $uploadList[0]['savename'];
		}
	}
	
	//查询请假列表
	public function search_leave(){
		if(!Database($_REQUEST["cid"]))exit;
		switch(''){
			case $_REQUEST["uid"]:return_json(40002);return;
			case $_REQUEST['conditions']:return_json(40039);return;
			case $_REQUEST['searchType']:return_json(40072);return;
			case $_REQUEST["count"]:$_REQUEST['count']=10;
			case $_REQUEST['page']:$_REQUEST['page']=1;
		}
		$rs=D('Leave')->search_leave($_REQUEST);
		if($rs){
			return_json('0',$rs);
		}else{
			return_json('40001');
		}	
	}
}
?>